import { ROAST_CONFIG } from '../config.js';
import { Message, MessageBox } from 'element-ui'
import axios from 'axios'
import store from '../store'
// 创建axios实例
const service = axios.create({
  baseURL: ROAST_CONFIG.API_URL,
  timeout: 15000 // 请求超时时间
})

// request拦截器
service.interceptors.request.use(config => {
  let token=store.getters.getSessionId;
  if(token){
     config.headers.Authorization=token;
  }
  return config
}, error => {
  console.log(error) // for debug
  return Promise.reject(error);
})

//resservice.interponse拦截器
service.interceptors.response.use(
  response => {
        //如果请求错误的情况
    if(response.status!=200){
        Message({
          message: '网络异常，请联系管理员',
          type: 'error',
          duration: 3 * 1000
        })
        return Promise.reject('error');
    }else{
       return response;
    }
  },
  error => {
      Message({
        message: '网络错误，请联系管理员',
        type: 'error',
        duration: 3 * 1000
      })
      return Promise.reject(error);
  }
)

export default service;
